package com.zelin.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.zelin.dao.GoodsDao;
import com.zelin.entity.GoodsDescEntity;
import com.zelin.entity.GoodsEntity;
import com.zelin.entity.ItemEntity;
import com.zelin.entity.group.Goods;
import com.zelin.sellergoods.service.*;
import com.zelin.utils.PageUtils;
import com.zelin.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {
    @Autowired
    private GoodsDescService goodsDescService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private ItemCatService itemCatService;
    @Autowired
    private SellerService sellerService;

    /**
     * 功能: 只显示未审核的
     * 参数:
     * 返回值: com.zelin.utils.PageUtils
     * 时间: 2021/7/30 16:28
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
                        .eq("audit_status","0")
        );
        return new PageUtils(page);
    }

    /**
     * 功能: 保存商品(组合对象)
     * 参数:
     * 返回值: void
     * 时间: 2021/7/29 14:54
     */
    @Override
    @Transactional
    public void saveGoods(Goods goods) {
        //1. 添加tb_goods表的数据
        goods.getGoods().setAuditStatus("0");       //代表未审核
        goods.getGoods().setSellerId("zelin");      //代表商家id
        this.save(goods.getGoods());

        //2. 添加tb_goodsDesc表的数据
        //2.1 设置tb_goodsDesc的主键
        goods.getGoodsDesc().setGoodsId(goods.getGoods().getId());
        //2.2 添加商品描述信息
        goodsDescService.save(goods.getGoodsDesc());
        insertItem(goods);

    }

    /**
     * 功能: 根据商品id查询复合类型商品
     * 参数:
     * 返回值: com.zelin.entity.group.Goods
     * 时间: 2021/7/29 21:47
     */
    @Override
    public Goods findById(Long id) {
        //1. 得到商品对象
        GoodsEntity goodsEntity = this.getById(id);
        System.out.println("goodsEntity = " + goodsEntity);
        //2. 得到商品描述对象
        GoodsDescEntity goodsDesc = goodsDescService.getById(id);
        System.out.println("goodsDesc = " + goodsDesc);
        //3. 查询sku商品列表
        List<ItemEntity> items = itemService.list(new QueryWrapper<ItemEntity>().eq("goods_id", id));
        System.out.println("items = " + items);
        //4. 定义复合对象
        Goods goods = new Goods();
        goods.setGoods(goodsEntity);
        goods.setGoodsDesc(goodsDesc);
        goods.setItems(items);
        //5. 返回
        return goods;
    }

    /**
     * 功能: 修改商品
     * 参数:
     * 返回值: void
     * 时间: 2021/7/30 15:35
     */
    @Override
    @Transactional
    public void update(Goods goods) {
        //1. 修改goods商品
        this.updateById(goods.getGoods());

        //2. 修改goodsDesc商品描述
        goodsDescService.updateById(goods.getGoodsDesc());

        //3. 对sku商品表，先根据goodsId这个外键删除
        itemService.remove(new QueryWrapper<ItemEntity>().eq("goods_id",goods.getGoods().getId()));

        //4. 添加sku商品列表
        this.insertItem(goods);
    }

    /**
     * 功能: 商品审核
     * 参数:
     * 返回值: void
     * 时间: 2021/7/30 16:13
     */
    @Override
    public void updateStatus(Long[] ids, String status) {
        for (Long id : ids) {
            //1. 根据id查询出商品
            GoodsEntity goods = this.getById(id);
            //2. 修改商品状态值
            goods.setAuditStatus(status);
            //3. 修改商品
            this.updateById(goods);
        }
    }

    /**
     * 功能: 添加sku商品
     * 参数:
     * 返回值: void
     * 时间: 2021/7/29 15:20
     */
    private void insertItem(Goods goods) {
        //3. 添加sku商品列表
        //3.1 得到sku商品列表
        List<ItemEntity> items = goods.getItems();
        //3.2 遍历sku商品列表添加信息
        for (ItemEntity item : items) {
            item.setGoodsId(goods.getGoods().getId());
            item.setCreateTime(new Date());
            item.setUpdateTime(new Date());
            //得到三级分类
            Long category3Id = goods.getGoods().getCategory3Id();
            //得到分类的名称
            String categoryName = itemCatService.getById(category3Id).getName();
            item.setCategoryid(category3Id);
            item.setCategory(categoryName);
            //得到商家id
            String sellerId = goods.getGoods().getSellerId();
            //得到商家名称
            String name = sellerService.getById(sellerId).getName();
            item.setSellerId(sellerId);
            item.setSeller(name);
            //设置标题
            item.setTitle(goods.getGoods().getGoodsName());
            //设置图像
            //得到上传的图像json串
            String itemImages = goods.getGoodsDesc().getItemImages();
            //转换为java的集合
            List<Map> maps = JSON.parseArray(itemImages, Map.class);
            //遍历此集合
             String url = (String) maps.get(0).get("url");
             //将url地址设置给sku对象
             item.setImage(url);

            //最后，保存sku商品
            itemService.save(item);
        }
    }

}